<?php

namespace App\Models;

use App\Http\Controllers\Admin\CommonController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * 图书到家banner管理
 */
class BookHomeBanner extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'book_home_banner';


    /**
     * 获取banner mode
     */
    public function getBannerType($id = null)
    {
        $data = [];
        $book_home_type = config('other.book_home_type');

        if (in_array(1, $book_home_type)) {
            $data[] = ['id' => 1, 'name' => '新书'];
        }
        if (in_array(2, $book_home_type)) {
            $data[] = ['id' => 2, 'name' => '馆藏书'];
        }
        if ($id) {
            $data = array_column($data, 'name', 'id');
            return !empty($data[$id]) ? $data[$id] : null;
        }
        return $data;
    }


    /**
     * 获取跳转内容的方式的数据
     * @param type   1 新书  2 馆藏书
     * @param keywords string 搜索关键词
     */
    public function getBannerTypeData($type, $keywords, $shop_id = null)
    {
        $book_home_type = config('other.book_home_type');

        if (!in_array($type, $book_home_type)) {
            return [];
        }

        $data = [];
        switch ($type) {
            case 1: // 新书
                $commonObj = new CommonController();
                $shop_all_id = $commonObj->getAdminShopIdAll();
                if (!empty($shop_all_id)) {
                    $shopBookModel = new ShopBook();
                    $field = ['n.id', 'n.book_name', 'n.author', 'n.press', 'n.pre_time', 'n.isbn', 'n.price', 'n.img'];
                    $data = $shopBookModel->lists($field, $shop_id, $keywords, null, null, null, null,  null,  null, null, $shop_all_id, null, 99);
                }
                break;
            case 2: // 馆藏书
                $libBookRecomModel = new LibBookRecom();
                $field = ['id', 'metaid', 'metatable', 'book_name', 'barcode', 'author', 'press', 'pre_time', 'type_id', 'isbn', 'book_num', 'price', 'intro', 'img'];
                $data = $libBookRecomModel->lists($field, null, null, $keywords, null, null, null, 99);
                break;
            default:
                $data = [];
        }
        return $data;
    }


    /**
     * 获取列表
     * @param limit int 分页大小
     * @param type int 显示位置（跳转位置）  1 新书  2 馆藏书
     * @param shop_id int 如果是新书，则需要显示是哪个书店的
     * @param skip_way 跳转方式  1、H5链接，2、应用内部  3 不跳转（默认3）
     * @param is_play    是否发布   1 发布  2 撤销
     * @param keywords string 搜索关键词
     */
    public function lists($search_data = [])
    {
        $keywords = $search_data['keywords'] ?? '';
        $skip_way = $search_data['skip_way'] ?? '';
        $is_play = $search_data['is_play'] ?? '';
        $type = $search_data['type'] ?? '';
        $shop_id = !empty($search_data['shop_id']) ? $search_data['shop_id'] : '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        $shop_all_id = !empty($search_data['shop_all_id']) ? $search_data['shop_all_id'] : '';

        $res = $this->select('id', 'name', 'img', 'link', 'is_play', 'sort', 'create_time', 'skip_way', 'type', 'shop_id')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('name', 'like', "%$keywords%");
                }
            })->where(function ($query) use ($type, $shop_id, $skip_way, $is_play, $shop_all_id) {
                if ($type) {
                    $query->where('type', $type);
                }
                if ($shop_id) {
                    $query->where('shop_id', $shop_id);
                }
                if ($shop_all_id) {
                    $query->whereIn('shop_id', $shop_all_id);
                }
                if ($skip_way) {
                    $query->where('skip_way', $skip_way);
                }
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })->where('is_del', 1)
            ->orderByDesc('sort')
            ->paginate($limit)
            ->toArray();

        return $res;
    }
    /**
     * 详情
     * @param id
     * @param is_play 是否发布
     */
    public function detail($id, $is_play = null)
    {
        $res = $this->select('id', 'name', 'img', 'link', 'is_play', 'sort', 'create_time', 'skip_way', 'type', 'shop_id')
            ->where('id', $id)->where(function ($query) use ($is_play) {
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })->where('is_del', 1)->first();
        if (empty($res)) {
            return false;
        }
        //获取对应数据记录
        if ($res['skip_way'] == 2) {
            $res['type_data'] = $this->getTypeData($res['type'], $res['link']);
            $res['type_name'] = $this->getBannerType($res['type']);
        }
        return $res;
    }

    /**
     *获取对应数据名称
     * @param type   1 新书  2 馆藏书
     * $id
     */
    public function getTypeData($type, $id)
    {
        if ($type == 1) {
            $shopBookModel = new ShopBook();
            $res['book_info'] = $shopBookModel->detail($id);
        } elseif ($type == 2) {
            $libBookRecomModel = new LibBookRecom();
            $res['book_info'] = $libBookRecomModel->detail($id);
        }
    }
}
